package com.ksyun.cloudhostalarmprocessing.service.Impl;

import com.ksyun.cloudhostalarmprocessing.common.QueryRequest;
import com.ksyun.cloudhostalarmprocessing.common.QueryResponse;
import com.ksyun.cloudhostalarmprocessing.common.Request;
import com.ksyun.cloudhostalarmprocessing.entity.Metrics;
import com.ksyun.cloudhostalarmprocessing.entity.vo.MetricsDataVO;
import com.ksyun.cloudhostalarmprocessing.entity.vo.MetricsVO;
import com.ksyun.cloudhostalarmprocessing.entity.vo.RateVO;
import com.ksyun.cloudhostalarmprocessing.mapper.MetricsMapper;
import com.ksyun.cloudhostalarmprocessing.service.IMetricsService;
import com.ksyun.cloudhostalarmprocessing.service.RealTimeComputingService;
import io.lettuce.core.StringMatchResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;

import java.time.ZoneId;
import java.util.*;
import java.util.stream.Collectors;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 李厚航
 * @since 2024-05-07
 */
@Service
public class MetricsServiceImpl implements IMetricsService {

    @Resource
    private RealTimeComputingService realTimeComputingService;

    @Resource
    private MetricsMapper metricsMapper;



    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Override
    @Async
    @Transactional
    public void add(List<Request> requests) {
        //将Request对象转化成Metrics对象
        List<Metrics> metrics = requests.stream().map(Request::toMetrics).collect(Collectors.toList());
        //将数据添加数据库
        metricsMapper.createBatch(metrics);
        //进行计算判断是否告警
        realTimeComputingService.calculate(metrics);
    }

    @Override
    public List<MetricsVO> selectLatest(String beginTime, String endTime) {
        return metricsMapper.selectLatest(beginTime,endTime);
    }

    @Override
    public RateVO selectRate(String beginTime, String endTime) {
        return metricsMapper.selectRate(beginTime,endTime);
    }

    @Override
    public List<Metrics> selectAll(Integer page,Integer size) {
        return metricsMapper.selectAll(page,size);
    }

    @Override
    public Integer selectCount() {
//        String key = "cache:metric";
//        String value = stringRedisTemplate.opsForValue().get(key);
//        if (StringUtils.isEmpty(value)){
//            Integer count = metricsMapper.selectCount();
//            stringRedisTemplate.opsForValue().set(key,String.valueOf(count));
//            return count;
//        }else {
//            return Integer.parseInt(value);
//        }
        return metricsMapper.selectCount();
    }

    @Override
    @Transactional
    public QueryResponse<MetricsDataVO> getByTimestamp(QueryRequest queryRequest) {
        List<MetricsDataVO> metrics = metricsMapper.selectByTimestamp(queryRequest.getStart(), queryRequest.getEnd());
        return new QueryResponse<>(queryRequest.getRequestId(),metrics.size(),metrics);
    }
}
